//
//  SFDetailsViewController.m
//  News
//
//  Created by dlios on 15-5-14.
//  Copyright (c) 2015年 PEAK. All rights reserved.
//

#import "SFDetailsViewController.h"
#import "UIImageView+WebCache.h"
#import "SNetWorkHandle.h"
#import "SDianyingBaseManager.h"
#import "Sshoucang.h"
@interface SFDetailsViewController ()<UITableViewDataSource, UITableViewDelegate, UIScrollViewDelegate>

//@property(nonatomic, retain)NSArray *arr;
@property(nonatomic, retain)NSMutableArray *ArrOne;
@property(nonatomic, retain)NSMutableArray *ArrTwo;
@property(nonatomic, retain)SfilmObject *sff;
@property(nonatomic, retain)UIImageView *images;
@property(nonatomic, retain)NSString *pubdate;
@property(nonatomic, assign)NSInteger average;
@property(nonatomic, retain)NSString *summary;
@property(nonatomic,retain)NSMutableDictionary *dic;

@property (nonatomic, retain)NSMutableArray *biaotiArr;
@property (nonatomic, retain)UIAlertView *al;
@property(nonatomic, retain) UIActivityIndicatorView *activityIndicator;
@property(nonatomic ,retain)UIImageView *imageView;

@end

@implementation SFDetailsViewController
- (void)dealloc
{
    [_img release];
    [_lable1 release];
    [_lable2 release];
    [_lable3 release];
    [_lable4 release];
    [_lable5 release];
    [_lable6 release];
    [_str release];
    [_large release];
    [_renshu release];
    [_guojia release];
    [_leixing release];
    [_xiamgqing release];
    [_shichang release];
    [_images release];
    [_pubdate release];
    [_summary release];
    [_dic release];
    [_biaotiArr release];
    [_al release];
    [_Mytitle release];
    
      [super dealloc];
}

- (void)viewWillAppear:(BOOL)animated
{
    self.tabBarController.tabBar.hidden = YES;
}
- (void)viewWillDisappear:(BOOL)animated
{
    self.tabBarController.tabBar.hidden = NO;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.biaotiArr = [NSMutableArray array];
    // Do any additional setup after loading the view.

    self.navigationController.navigationBar.translucent = NO;
    self.automaticallyAdjustsScrollViewInsets = NO;

   self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"iconfont-guanwangicon31332-2"] style:UIBarButtonItemStyleDone target:self action:@selector(gg)];
    self.title = self.Mytitle;
    
    [self details];
    
}

-(void)details
{
    
    
    //请求出现小菊花
    self.imageView  = [[UIImageView alloc] initWithFrame:self.view.bounds];
    _imageView.image = [UIImage imageNamed:@"5EA2D64D-15BE-43D8-AB18-E15BCE039C8F"];
    _imageView.alpha =0.6;
    [self.view addSubview:_imageView];
    
    _activityIndicator = [[UIActivityIndicatorView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 32.0f, 32.0f)];
    [_activityIndicator setCenter:_imageView.center];
    _activityIndicator.backgroundColor= [UIColor grayColor];
    [_activityIndicator setActivityIndicatorViewStyle:UIActivityIndicatorViewStyleWhite];
    [_imageView addSubview:_activityIndicator];
    
    [_activityIndicator startAnimating];

    
    if (self.xiamgqing.length == 0) {
        
    
    
    NSString *str = [NSString stringWithFormat:@"http://api.douban.com/v2/movie/subject/%@?alt=json&apikey=0df993c66c0c636e29ecbb5344252a4a&app_name=doubanmovie&city=",_str];
    
    
    
    
    [SNetWorkHandle getDataWithURL:str completion:^(id result) {

        self.dic = result;
        
               self.xiamgqing = [_dic objectForKey:@"summary"];
        NSString *f = @"";
        for (NSString *s10 in [_dic objectForKey:@"genres"]) {
            
            f = [NSString stringWithFormat:@"%@ %@",f,s10];

        }
        self.leixing = f;
        self.large = [[_dic objectForKey: @"images"] objectForKey:@"large"];
        NSString *s  = [NSString stringWithFormat:@"%@",[[_dic objectForKey:@"rating"] objectForKey:@"average"] ];
        self.pingfen = [s integerValue];
        NSString *S = [NSString stringWithFormat:@"%@",[_dic objectForKey:@"ratings_count"]];
        self.renshu = S;
        NSString *a = [NSString stringWithFormat:@"%@",[_dic objectForKey:@"pubdate"]];
        NSString *d = [NSString stringWithFormat:@"%@",[[_dic objectForKey:@"durations"] lastObject]];
        self.shichang = [NSString stringWithFormat:@"%@ / %@",a,d];
        NSString *e = [NSString stringWithFormat:@"%@",[[_dic objectForKey:@"countries"] lastObject]];
        self.guojia = e;
        [self create];
        [_imageView removeFromSuperview];

    }];

    }
    
    else if (self.xiamgqing.length != 0){
        [self create];
        
    }
}
-(void)create
{
    
    UIScrollView *scroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height  - 50)];
    scroll.backgroundColor = [UIColor whiteColor];
    scroll.delegate  = self;
    
    [self.view addSubview:scroll];
    
    self.img = [[UIImageView alloc] init];
    _img.frame = CGRectMake(10, 30, [UIScreen mainScreen].bounds.size.width / 3.125, [UIScreen mainScreen].bounds.size.height / 4.45);
    
    [_img sd_setImageWithURL:[NSURL URLWithString:_large]placeholderImage:[UIImage imageNamed:@"``_3(NZ4U{L@4L7FN0J70DA"]];
    [scroll addSubview:_img];
    [_img release];
    
    self.lable1 = [[UILabel alloc] init];
//        _lable1.backgroundColor = [UIColor yellowColor];
    _lable1.frame = CGRectMake([UIScreen mainScreen].bounds.size.width / 2.55, 40,[UIScreen mainScreen].bounds.size.width / 5.3, 20);
   
    _lable1.textColor = [UIColor redColor];
    NSString *str = [NSString stringWithFormat:@"%ld", (long)self.pingfen];
    
    _lable1.text = str;
    [scroll addSubview:_lable1];
    [_lable1 release];
    
    self.lable2 = [[UILabel alloc] init];
//        _lable2.backgroundColor = [UIColor orangeColor];
    _lable2.frame = CGRectMake([UIScreen mainScreen].bounds.size.width / 1.6, 40, [UIScreen mainScreen].bounds.size.width - 210 , 20);
    NSString *s = [NSString stringWithFormat:@"%@人评分",_renshu];
    _lable2.text = s;

    [scroll addSubview:_lable2];
    [_lable2 release];
    
    self.lable3 = [[UILabel alloc] init];
//        _lable3.backgroundColor =[ UIColor yellowColor];
    _lable3.frame = CGRectMake([UIScreen mainScreen].bounds.size.width / 2.55, [UIScreen mainScreen].bounds.size.height / 8.5, [UIScreen mainScreen].bounds.size.width  - 150 , 20);
    _lable3.font = [UIFont systemFontOfSize:15];
       _lable3.text = _shichang;
    [scroll addSubview:_lable3];
    [_lable3 release];
    
    
    
    self.lable4  =[[UILabel alloc] init];
    //    _lable4.backgroundColor  =[UIColor greenColor];
    _lable4.frame = CGRectMake([UIScreen mainScreen].bounds.size.width / 2.55, [UIScreen mainScreen].bounds.size.height / 6, [UIScreen mainScreen].bounds.size.width / 4, 20);
   
    _lable4.text = _guojia;
    [scroll addSubview:_lable4];
    [_lable4 release];
    
    self.lable5 = [[UILabel alloc] init];
//        _lable5.backgroundColor =[UIColor cyanColor];
    _lable5.frame = CGRectMake([UIScreen mainScreen].bounds.size.width / 2.65, [UIScreen mainScreen].bounds.size.height / 4.65, [UIScreen mainScreen].bounds.size.width / 2.3, 20);
       _lable5.text  = _leixing;
    _lable5.font = [UIFont systemFontOfSize:17];
    [scroll addSubview:_lable5];
    [_lable5 release];
    
    
    
    
    self.lable6 = [[UILabel alloc] init];
    _lable6.text  = self.xiamgqing;
       //    _lable6.backgroundColor = [UIColor yellowColor];
    CGFloat f3 = [[SFDetailsViewController class] heightWithText:_lable6.text];
    _lable6.frame = CGRectMake(10, [UIScreen mainScreen].bounds.size.height / 3.6, [UIScreen mainScreen].bounds.size.width - 20, f3 + 240);
    _lable6.font = [UIFont systemFontOfSize:18];
    _lable6.numberOfLines = 0;
    [scroll addSubview:_lable6];
    [_lable6 release];
    [scroll release];

    scroll.contentSize = CGSizeMake(0, _lable6.frame.size.height + _img.frame.size.height );
    
    [_imageView removeFromSuperview];

}

+(CGFloat)heightWithText:(NSString *)text
{
    NSDictionary *dic = @{NSFontAttributeName:[UIFont systemFontOfSize:17]};
    CGSize size = CGSizeMake(375, 1000);
    CGRect rect = [text boundingRectWithSize:size options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil];
    return rect.size.height;
    
    
}
-(void)gg
{
    
    SDianyingBaseManager *dbManager = [SDianyingBaseManager shareInstance];
    [dbManager openDB];
    
    [dbManager createTable];
    
    Sshoucang *shoucang = [[[Sshoucang alloc] init] autorelease];
    
    shoucang.xiamgqing = _xiamgqing;
    shoucang.shichang = _shichang;
    shoucang.renshu = _renshu;
    shoucang.pingfen = _pingfen;
    //NSLog(@"====%ld",(long)_pingfen);
    shoucang.guojia = _guojia;
    shoucang.large = _large;
    shoucang.leixing = _leixing;
    shoucang.Mytitle = self.Mytitle;
    NSArray *arr = [NSArray array];
    arr = [dbManager selectInfo];
    
    for (Sshoucang *shoucang in arr) {
        [_biaotiArr addObject:shoucang.Mytitle];
        
    }
    if (![_biaotiArr containsObject:shoucang.Mytitle]) {
        [dbManager insertInfoWithSshoucang:shoucang];
        
        self.al = [[UIAlertView alloc] initWithTitle:@"提示" message:@"收藏成功" delegate:self cancelButtonTitle:nil otherButtonTitles:nil, nil];
        [self.view addSubview:_al];
        [_al show];
        [NSTimer scheduledTimerWithTimeInterval:0.3 target:self selector:@selector(lisong) userInfo:nil repeats:NO];
    }else {
        self.al = [[UIAlertView alloc] initWithTitle:@"提示" message:@"已收藏" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        [self.view addSubview:_al];
        [_al show];
    }
}
- (void)lisong
{
    [_al dismissWithClickedButtonIndex:0 animated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
